import { TbRoot } from "../../../platform/excel_table/TbRoot";
import { replacer, reviver } from "../../../platform/utils/TypeUtil";
import { AppCfg } from "../../AppCfgs";
import { Enum_GameIds, Game_Levels } from "../../common/enums";
import { GameLevelInfo } from "../../common/GameLevelInfo"
import { GameCtx } from "../../GameCtx";
import { Tb_GameListContainer } from "../../tb/Tb_GameList";
import { BaseData } from "../player/BaseData";
import { PlayerData } from "../player/PlayerData";

export class GameWuziqi_Data extends BaseData{
    levelInfos:Map<number,GameLevelInfo> = new Map()

    constructor(parent:PlayerData){
        super()
        this.parent = parent
    }

    loadData(): void {
        let fullKey = this.getDataFullKey()
        let localData = localStorage.getItem(fullKey)
        if(!localData){
            this.initLevelDatas()
            return
        }
        let data = JSON.parse(localData,reviver) as Map<number,GameLevelInfo>
        if(data){
            this.levelInfos = data
        }else{
            this.initLevelDatas()
        }

    }
    initLevelDatas() {
        let container = TbRoot.ins.getTbContainer(Tb_GameListContainer)
        let cfg = container.getCfgById(Enum_GameIds.GameWuziqi)
        cfg.levels.forEach(level => {
                //初始化数据
            this.levelInfos.set(level,new GameLevelInfo(level));
        })
    }

    getDataKey(): string {
        return "GameWuziqi_Data"
    }

    saveData(): void {
        if(GameCtx.ins.dataFrom==="local"){
            localStorage.setItem(this.getDataFullKey(),JSON.stringify(this.levelInfos,replacer))
            return
        }
        //TODO 发请求保存
    }
}